var WxParse = require('../../lib/wxParse/wxParse.js');
var util = require('../../utils/util.js');
const app = getApp();

Page({
  data: {
    height: 64, //header高度
    top: 0, //标题图标距离顶部距离
    opcity: 0,
    scrollH: 0, //滚动总高度
    iconOpcity: 0.5,
    id: 0,
    topic: {},
    topicList: [],
    commentCount: 0,
    commentList: []
  },
  onShareAppMessage: function() {
    var that = this;
    return {
      title: that.data.topic.title,
      desc: that.data.topic.subtitle,
      path: '/pages/topicDetail/topicDetail?id=' + that.data.topic.id
    }
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    that.setData({
      id: options.id,
      width: app.globalData.customBar.width,
      height: app.globalData.customBar.height,
      top: app.globalData.customBar.top,
      scrollH: app.globalData.customBar.scrollH
    })
    util.request('topic/detail', { id: that.data.id}).then(function (res) {
      if (res.code === 0) {
        that.setData({
          topic: res.data,
        });
        WxParse.wxParse('topicDetail', 'html', res.data.content, that);
      }
    });

    util.request('topic/related', { id: that.data.id}).then(function (res) {
      if (res.code === 0) {
        that.setData({
          topicList: res.data.records
        });
      }
    });
  },
  back: function () {
    wx.navigateBack({
      fail() {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
  },
  getCommentList(){
    let that = this;
    util.request('comment/list', { goodsId: that.data.id, type: 1, size: 5 ,showType: 0}).then(function (res) {
      if (res.code === 0) {
        that.setData({
          commentList: res.data.records,
          commentCount: res.data.total
        });
      }
    });
  },
  postComment (){
    wx.navigateTo({
      url: '/pages/commentPost/commentPost?orderId='+this.data.id + '&type=1',
    })
  },
  onPageScroll(e) {
    let scroll = e.scrollTop <= 0 ? 0 : e.scrollTop;
    let opcity = scroll / this.data.scrollH;
    if (this.data.opcity >= 1 && opcity >= 1) {
      return;
    }
    this.setData({
      opcity: opcity,
      iconOpcity: 0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
    })
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
    this.getCommentList();
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})
